package com.bbdjt.weixin.web.action.leaveWord;


import org.apache.log4j.Logger;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.LeaveWord;
import com.bbdjt.weixin.model.LeaveWordQueryModel;
import com.bbdjt.weixin.service.LeaveWordService;
import com.bbdjt.weixin.web.action.forumPost.ForumPostAction;

/**
 * @项目名称: bbdjt
 * @类名称: LeaveWordAction
 * @类描述: LeaveWord表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-03-20 12:05:24
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class LeaveWordAction extends BaseAction {
	
	@Autowired
	private LeaveWordService leaveWordService;

	private String page_list = "jsp/leaveWord/leaveWordindex";

	private String redirect_list = "redirect:r_leaveWord";
	
	private static Logger logger = Logger.getLogger(LeaveWordAction.class);
	
	
	// 分页查询
	@RequestMapping(value = "r_leaveWord")
	public ModelAndView getListPage(LeaveWordQueryModel leaveWord) {
		ModelAndView result = new ModelAndView(page_list);
		leaveWord.setOrderBy("id asc");
		List<LeaveWord> leaveWordList = leaveWordService.selectByLeaveWord(leaveWord);
		result.addObject("pageInfo", new PageInfo<LeaveWord>(leaveWordList));
		result.addObject("leaveWord", leaveWord);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_leaveWordXml")
	public ModelAndView getListPageXml(LeaveWordQueryModel leaveWord) {
		ModelAndView result = new ModelAndView(page_list);
		leaveWord.setOrderBy("id asc");
		PageInfo<LeaveWordQueryModel> pageInfo = leaveWordService.selectByLeaveWordQueryModel(leaveWord);
		List<LeaveWordQueryModel> leaveWordList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<LeaveWordQueryModel>(leaveWordList));
		result.addObject("leaveWord", leaveWord);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_leaveWordJson", produces = "application/json")
	public ModelAndView pageInfo(LeaveWordQueryModel leaveWord) {
		ModelAndView result = new ModelAndView();
		PageInfo<LeaveWordQueryModel> pageInfo = leaveWordService.selectByLeaveWordQueryModel(leaveWord);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_leaveWordLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(LeaveWordQueryModel leaveWord) {
		leaveWord.setOrderBy(leaveWord.getSortname(),leaveWord.getSortorder());
		PageInfo<LeaveWordQueryModel> pageInfo = leaveWordService.selectByLeaveWordQueryModel(leaveWord);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_leaveWord")
	public ModelAndView add(LeaveWord leaveWord) {
		ModelAndView result = new ModelAndView();
		if (leaveWord.getId() != null) {
			leaveWord = leaveWordService.selectByKey(leaveWord.getId());
		}
		result.addObject("leaveWord", leaveWord);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_leaveWord")
	public ModelAndView save(LeaveWord leaveWord) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (leaveWord.getId() != null) {
			leaveWordService.updateNotNull(leaveWord);
		} else {
			// 会保存null 值
			leaveWordService.insertSelective(leaveWord);
		}
		return result;
	}

	// 删除数据
	@RequestMapping("d_leaveWord")
	public String delete(Integer id) {
		leaveWordService.delete(id);
		return redirect_list;
	}
	
	//患者留言回复
	@ResponseBody
	@RequestMapping(value = "addLeaveWord")
	public String addLeaveWord (LeaveWord leaveWord){
		if(leaveWord.getPatientId() != null && leaveWord.getLeaveContent() != null && leaveWord.getForumPostId() != null){
			leaveWordService.insertSelective(leaveWord);
			return super.writeOK(leaveWord);
		}else{
			logger.info("患者ID或留言内容或帖子ID为空:" + leaveWord.getPatientId() + "," + leaveWord.getLeaveContent() + "," + leaveWord.getForumPostId());
			return super.lackError();
		}
	}
}